import { message } from 'antd';
import {
  getDictList,
  getCorpList,
  getChildList,
  getBusiParam,
  saveBusiParam,
} from '@/services/busiparamset';
import { getMatList } from '@/services/inservicematerial';

export default {
  state: {
    treeData: [],
    data: {},
    dictData: {},
    selectedTreeKeys: [],
    comFlag: true,
    pageInfo: {},
    corpId: '',
    execCorpId: '',
    corpName: '',
    bigResult: [],
  },
  effects: {
    // 获取字典
    *fetchDictList({ payload }, { call, put }) {
      const response = yield call(getDictList, payload);
      if (response.success) {
        yield put({
          type: 'getDictData',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    *fetchCorpList({ payload }, { call, put }) {
      const response = yield call(getCorpList, payload);

      if (response.success) {
        yield put({
          type: 'getTreeData',
          payload: response.data,
        });
        if (response.data.length) {
          const responseData = yield call(getBusiParam, { execCorpId: response.data[0].corpId });
          if (responseData.success) {
            yield put({
              type: 'getPageInfoData',
              payload: responseData.data,
            });
          } else {
            message.warning(responseData.message || '服务器错误');
          }
        }
      } else {
        message.warning(response.message);
      }
    },
    *fetchChildList({ payload }, { call, put }) {
      const response = yield call(getChildList, payload);
      const { corpId } = payload;
      if (response.success) {
        yield put({
          type: 'getChildrenData',
          payload: { ...response, corpId },
        });
        return response.data;
      }
      return message.warning(response.message || '服务器错误');
    },
    *fetchBusiParam({ payload }, { call, put }) {
      const responseData = yield call(getBusiParam, payload);
      if (responseData.success) {
        yield put({
          type: 'getPageInfoData',
          payload: responseData.data,
        });
      } else {
        message.warning(responseData.message || '服务器错误');
      }
    },
    *saveParam({ payload }, { call, put, select }) {
      const response = yield call(saveBusiParam, payload);
      if (response.success) {
        const execCorpId = yield select(state => state.busiparamset.selectedTreeKeys[0]);
        const responseData = yield call(getBusiParam, { execCorpId });
        if (responseData.success) {
          yield put({
            type: 'getPageInfoData',
            payload: responseData.data,
          });
        } else {
          message.warning(responseData.message || '服务器错误');
        }
        message.success('保存成功！');
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    // 获取材料
    *fetchMatList({ payload }, { call, put }) {
      const response = yield call(getMatList, payload);
      if (response.success) {
        yield put({
          type: 'getMatList',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },
  },

  reducers: {
    // 获取字典数据
    getDictData(state, { payload }) {
      return {
        ...state,
        dictData: payload,
      };
    },
    // 获取树形数据
    getTreeData(state, { payload }) {
      const treeData = payload.map(item => ({
        title: item.companyName,
        key: item.corpId,
        corpName: item.companyName, // 区分父级和子级
      }));
      return {
        ...state,
        treeData,
        selectedTreeKeys: [payload[0].corpId],
        corpId: payload[0].corpId,
        execCorpId: payload[0].corpId,
        corpName: payload[0].companyName,
      };
    },
    getChildrenData(state, { payload }) {
      const childrenData = payload.data.map(item => ({
        title: item.name,
        key: `${item.orgId}`,
        dataInfo: item,
        name: item.name,
        isLeaf: payload.data.isLeaf || Number(item.functionType) === 1,
      }));
      const treeData = state.treeData.map(item => ({
        ...item,
        children: item.key === payload.corpId ? childrenData : item.children,
      }));
      return {
        ...state,
        treeData,
      };
    },
    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    getPageInfoData(state, { payload }) {
      const pageInfo = {};
      // let newArr = [];
      Object.keys(payload).forEach(key => {
        if (key === 'autoDispch' || key === 'autoRcvr' || key === 'billGenRule') {
          pageInfo[key] = `${payload[key]}`;
        } else if (key === 'template' || key === 'templateName') {
          pageInfo[key] = payload[key] && payload[key].split(',');
          // newArr = pageInfo.template.map((item, i) => ({
          //   matId: item,
          //   matName: pageInfo.templateName && pageInfo.templateName[i],
          // }));
        } else {
          pageInfo[key] = payload[key];
        }
      });
      return {
        ...state,
        pageInfo,
        // bigResult: newArr,
      };
    },
    getMatList(state, { payload }) {
      return {
        ...state,
        bigResult: payload,
      };
    },
  },
};
